package com.elitesland.demo.controller.backend.com;

import com.elitesland.b2c.com.service.ComFileService;
import com.elitesland.b2c.com.vo.resp.ComFileUploadRespVO;
import com.elitesland.core.base.ApiResult;
import com.elitesland.demo.controller.BaseController;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * .
 *
 * @author Kaiser（wang shao）
 * @date 2021-02-09
 */
@Api(tags = "文件服务")
@ApiSupport(author = "wang shao", order = 1)
@RestController
@RequestMapping(value = "/com/file", produces = MediaType.APPLICATION_JSON_VALUE)
public class ComFileController extends BaseController {

    @Autowired
    private ComFileService fileService;

    @ApiOperation(value = "上传单文件")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "要上传的文件", required = true, dataTypeClass = MultipartFile.class)
    })
    @PostMapping(value = "/upload")
    public ApiResult<ComFileUploadRespVO> upload(MultipartFile file) {
        return fileService.upload(file);
    }

    @ApiOperation(value = "显示图片")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文件ID", required = true),
            @ApiImplicitParam(name = "thumbnail", value = "是否预览缩略图", defaultValue = "true"),
    })
    @GetMapping(value = "/{id}/show")
    public ApiResult<String> show(@PathVariable Long id, Boolean thumbnail, HttpServletResponse response) throws Exception {
        return fileService.show(response.getOutputStream(), id, thumbnail);
    }

    @ApiOperation(value = "下载文件")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文件ID", required = true)
    })
    @GetMapping(value = "/{id}/download")
    public ApiResult<Long> download(@PathVariable Long id, HttpServletResponse response) {
        try {
            return fileService.download(id, response.getOutputStream());
        } catch (IOException e) {
            logger.error("下载文件失败:", e);
            return ApiResult.fail("下载文件失败！");
        }
    }

    @ApiOperation(value = "删除文件")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文件ID", required = true)
    })
    @DeleteMapping(value = "/{id}")
    public ApiResult<Long> delete(@PathVariable Long id) {
        return fileService.delete(id);
    }

    @ApiOperation(value = "批量删除文件")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "文件ID", required = true, dataTypeClass = Long.class, allowMultiple = true)
    })
    @DeleteMapping(value = "/delete")
    public ApiResult<List<Long>> deleteByIdBatch(@RequestBody List<Long> ids) {
        return fileService.delete(ids);
    }
}
